package tools.oracle;


import java.io.File;
import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.zhixiang.core.util.DateUtil;



/**
 * @author whw
 * @version v1.0
 * @date 2016/5/21
 * 导入数据库脚本(doc/sql目录下的.sql文件)工具类
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:conf/app-context.xml"})
public class InitOracle
{
    private Date beforeTime;
    private Date afterTime;


    @Autowired
    private SessionFactory sessionFactory;

    /**
     * 初始化doc/sql目录下的所有.sql后缀的脚本
     * Description: <br>
     * @see
     */
    @Test
    public void initSqlTest()
    {
        //        String baseDir = System.getProperty("user.dir") + "/doc/sql/04procedure";
        String baseDir = System.getProperty("user.dir") + "/doc/sql/oracle";
        String oraclHome = DBUtils.getOracleHome(sessionFactory);
        String userName = DBUtils.getUserName(sessionFactory);
        File baseFile = new File(baseDir);
        List<File> files = DBUtils.getFiles(baseFile);

        for (int i = 0; i < files.size(); i++)
        {
            File file = files.get(i);
            System.out.println("---------导入开始" + (i + 1) + "---------" + file.getName());
            DBUtils.oracleImport(oraclHome, userName, file.getAbsolutePath());
            System.out.println("---------结束导入" + (i + 1) + "---------\n");
        }
    }



    @Before
    public void before()
    {
        beforeTime = new Date();
        System.out.println("---------------------" + DateUtil.getDateStr(beforeTime,"yyyy-MM-dd HH:mm:ss:SSS") + "---------------------");
    }

    @After
    public void after()
    {
        afterTime = new Date();
        System.out.println("---------------------" + DateUtil.getDateStr(afterTime, "yyyy-MM-dd HH:mm:ss:SSS") + "---------------------");
        Long time = afterTime.getTime() - beforeTime.getTime();
        System.err.println("==============================hazy恭喜您，初始化数据完成，共用时" + time + "毫秒==================================");
    }
}
